package com.alkin.thing.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 * @auther shanjunpeng 2019/11/6
 */
public class PingUtil {

    public static boolean ping(String ip) {
        boolean flag = false;
        BufferedReader br = null;
        try {
            Process p1 = Runtime.getRuntime().exec("ping -n 3 " + ip);
            br = new BufferedReader(new InputStreamReader(p1.getInputStream()));
            String line = null;

            while ((line = br.readLine()) != null) {
                if (line.contains("TTL=")) { // 只要有一次返回成功，就算在线
                    flag = true;
                    break;
                }
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 能否ping通
     *
     * @param ip ip地址
     * @return true 可以  false 不可以
     */
    public static boolean ping2(String ip) {
        if (StringUtils.isEmpty(ip)) {
            return false;
        }
        int pingCount = 4;
        int timeOut = 2000;
        String command = "ping -n " + pingCount + " -w " + timeOut + " " + ip;
        BufferedReader localBufferedReader = null;
        try {
            Runtime localRuntime = Runtime.getRuntime();
            Process localProcess = localRuntime.exec(command);
            localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream(), "GBK"));
            int TTLCount = 0;
            for (String line = localBufferedReader.readLine(); line != null;
                 line = localBufferedReader.readLine()) {
                if (line.toUpperCase().contains("TTL=")) {
                    TTLCount++;
                }
            }
            if (TTLCount > 0) {
                return true;
            }

        } catch (IOException e) {
            return false;
        } finally {
            if (localBufferedReader != null) {
                try {
                    localBufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

    public static boolean ping3(String ip) {
        boolean status = false;     // 当返回值是true时，说明host是可用的，false则不可。
        try {
            status = InetAddress.getByName(ip).isReachable(2000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return status;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        System.out.println(ping("192.168.3.241"));
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        System.out.println(ping2("192.168.3.241"));
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        System.out.println(ping3("192.168.3.241"));
        System.out.println(System.currentTimeMillis() - start);
    }
}
